var _excluded = ['pathname', 'params', 'base', 'routes', 'mode', 'clickable', 'empty'];
function _extends() {
  _extends = Object.assign
    ? Object.assign.bind()
    : function (target) {
        for (var i = 1; i < arguments.length; i++) {
          var source = arguments[i];
          for (var key in source) {
            if (Object.prototype.hasOwnProperty.call(source, key)) {
              target[key] = source[key];
            }
          }
        }
        return target;
      };
  return _extends.apply(this, arguments);
}
function _objectWithoutProperties(source, excluded) {
  if (source == null) return {};
  var target = _objectWithoutPropertiesLoose(source, excluded);
  var key, i;
  if (Object.getOwnPropertySymbols) {
    var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
    for (i = 0; i < sourceSymbolKeys.length; i++) {
      key = sourceSymbolKeys[i];
      if (excluded.indexOf(key) >= 0) continue;
      if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
      target[key] = source[key];
    }
  }
  return target;
}
function _objectWithoutPropertiesLoose(source, excluded) {
  if (source == null) return {};
  var target = {};
  var sourceKeys = Object.keys(source);
  var key, i;
  for (i = 0; i < sourceKeys.length; i++) {
    key = sourceKeys[i];
    if (excluded.indexOf(key) >= 0) continue;
    target[key] = source[key];
  }
  return target;
}
/*
 * @Descripttion:
 * @Author: gll
 * @Date: 2024-03-06 19:14:42
 * @LastEditors: gll
 * @LastEditTime: 2024-03-07 15:09:10
 */
import HomeOutlined from '@ant-design/icons/HomeOutlined';
import React, { useMemo } from 'react';
import { generatePath, Link } from 'react-router-dom';
import ComplexBreadcrumbs from './ComplexBreadcrumbs';
import SimpleBreadcrumbs from './SimpleBreadcrumbs';
import useMatches from './useMatches';

/**
 * 面包屑
 * 需要保证传入 routes，没有 path 相同的配置项，即没有同名 redirect
 * @param param0
 * @returns
 */
function Breadcrumbs(_ref) {
  var _matches;
  var _ref$pathname = _ref.pathname,
    pathname = _ref$pathname === void 0 ? '/' : _ref$pathname,
    params = _ref.params,
    base = _ref.base,
    routes = _ref.routes,
    _ref$mode = _ref.mode,
    mode = _ref$mode === void 0 ? 'simple' : _ref$mode,
    _ref$clickable = _ref.clickable,
    clickable = _ref$clickable === void 0 ? true : _ref$clickable,
    _ref$empty = _ref.empty,
    empty = _ref$empty === void 0 ? null : _ref$empty,
    rest = _objectWithoutProperties(_ref, _excluded);
  var matches = useMatches({
    pathname: pathname,
    base: base,
    routes: routes,
  });
  var breadcrumbItems = useMemo(
    function () {
      var items = clickable
        ? matches.map(function (route) {
            return {
              path: route.path,
              title:
                route.target || generatePath(route.path, params) !== route.path
                  ? route.title
                  : /*#__PURE__*/ React.createElement(
                      Link,
                      {
                        to: route.path,
                      },
                      route.path === '/' && !route.title
                        ? /*#__PURE__*/ React.createElement(HomeOutlined, null)
                        : route.title,
                    ),
              onClick: function onClick() {
                if (route.target) {
                  window.location.href = route.target;
                }
              },
              route: route,
            };
          })
        : matches.map(function (route) {
            return {
              path: route.path,
              title: route.title,
              route: route,
            };
          });
      return items;
    },
    [matches, params],
  );
  if (
    !matches.length ||
    ((_matches = matches[matches.length - 1]) === null || _matches === void 0
      ? void 0
      : _matches.breadcrumb) === false
  ) {
    return empty;
  }
  return mode === 'simple'
    ? /*#__PURE__*/ React.createElement(
        SimpleBreadcrumbs,
        _extends({}, rest, {
          items: breadcrumbItems,
        }),
      )
    : /*#__PURE__*/ React.createElement(
        ComplexBreadcrumbs,
        _extends({}, rest, {
          items: breadcrumbItems,
        }),
      );
}
Breadcrumbs.SimpleBreadcrumbs = SimpleBreadcrumbs;
Breadcrumbs.ComplexBreadcrumbs = ComplexBreadcrumbs;
export default Breadcrumbs;
